package com.ruoyi.project.business.batch.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.batch.mapper.YlPickCodeBatchMapper;
import com.ruoyi.project.business.batch.domain.YlPickCodeBatch;
import com.ruoyi.project.business.batch.service.IYlPickCodeBatchService;
import com.ruoyi.project.business.pickCode.domain.YlPickCode;
import com.ruoyi.project.business.pickCode.service.IYlPickCodeService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 取货码批次Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-26
 */
@Service
public class YlPickCodeBatchServiceImpl implements IYlPickCodeBatchService 
{
    @Autowired
    private YlPickCodeBatchMapper ylPickCodeBatchMapper;
    @Autowired
    private IYlPickCodeService ylPickCodeService;

    /**
     * 查询取货码批次
     * 
     * @param batchId 取货码批次ID
     * @return 取货码批次
     */
    @Override
    public YlPickCodeBatch selectYlPickCodeBatchById(Long batchId)
    {
        return ylPickCodeBatchMapper.selectYlPickCodeBatchById(batchId);
    }

    /**
     * 查询取货码批次列表
     * 
     * @param ylPickCodeBatch 取货码批次
     * @return 取货码批次
     */
    @Override
    public List<YlPickCodeBatch> selectYlPickCodeBatchList(YlPickCodeBatch ylPickCodeBatch)
    {
        return ylPickCodeBatchMapper.selectYlPickCodeBatchList(ylPickCodeBatch);
    }

    /**
     * 新增取货码批次
     * 
     * @param ylPickCodeBatch 取货码批次
     * @return 结果
     */
    @Override
    public int insertYlPickCodeBatch(YlPickCodeBatch ylPickCodeBatch)
    {
        ylPickCodeBatch.setCreateTime(DateUtils.getNowDate());
        int iReturn = ylPickCodeBatchMapper.insertYlPickCodeBatch(ylPickCodeBatch);
        //产生取货码
        for(int i=0;i<ylPickCodeBatch.getPickCodeQuantity();i++) {
        	YlPickCode ylPickCode=new YlPickCode();
        	ylPickCode.setBatchId(ylPickCodeBatch.getBatchId());
        	ylPickCode.setMachineId(ylPickCodeBatch.getMachineId());
        	ylPickCode.setMachineName(ylPickCodeBatch.getMachineName());
        	ylPickCode.setProductId(ylPickCodeBatch.getProductId());
        	ylPickCode.setProductName(ylPickCodeBatch.getProductName());
        	ylPickCode.setEffectiveStartDate(ylPickCodeBatch.getEffectiveStartDate());
        	ylPickCode.setEffectiveEndDate(ylPickCodeBatch.getEffectiveEndDate());
        	ylPickCode.setStatus(1L);
        	int iGResult = ylPickCodeService.generateYlPickCode(ylPickCode);
        }
        return iReturn;
    }

    /**
     * 修改取货码批次
     * 
     * @param ylPickCodeBatch 取货码批次
     * @return 结果
     */
    @Override
    public int updateYlPickCodeBatch(YlPickCodeBatch ylPickCodeBatch)
    {
        ylPickCodeBatch.setUpdateTime(DateUtils.getNowDate());
        return ylPickCodeBatchMapper.updateYlPickCodeBatch(ylPickCodeBatch);
    }

    /**
     * 删除取货码批次对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteYlPickCodeBatchByIds(String ids)
    {
        return ylPickCodeBatchMapper.deleteYlPickCodeBatchByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除取货码批次信息
     * 
     * @param batchId 取货码批次ID
     * @return 结果
     */
    @Override
    public int deleteYlPickCodeBatchById(Long batchId)
    {
        return ylPickCodeBatchMapper.deleteYlPickCodeBatchById(batchId);
    }
    
    /**
     * 批量停用取货码批次
     * 
     * @param ids 需要停用的数据ID
     * @return 结果
     */
    @Override
    public int blockYlPickCodeBatchByIds(String ids) {
    	String[] batchIds = Convert.toStrArray(ids);
    	for(String batchId:batchIds) {
    		//改批次的状态为停用
    		YlPickCodeBatch ylPickCodeBatch = ylPickCodeBatchMapper.selectYlPickCodeBatchById(Long.valueOf(batchId));
    		ylPickCodeBatch.setStatus(0L);
    		ylPickCodeBatchMapper.updateYlPickCodeBatch(ylPickCodeBatch);
    		//停用该批次的取货码
    		ylPickCodeService.blockYlPickCodeByBatchId(Long.valueOf(batchId));
    	}
    	return 1;
    }
    
    /**
     * 批量启用取货码批次
     * 
     * @param ids 需要停用的数据ID
     * @return 结果
     */
    @Override
    public int activeYlPickCodeBatchByIds(String ids) {
    	String[] batchIds = Convert.toStrArray(ids);
    	for(String batchId:batchIds) {
    		//改批次的状态为启用
    		YlPickCodeBatch ylPickCodeBatch = ylPickCodeBatchMapper.selectYlPickCodeBatchById(Long.valueOf(batchId));
    		ylPickCodeBatch.setStatus(1L);
    		ylPickCodeBatchMapper.updateYlPickCodeBatch(ylPickCodeBatch);
    		//停用该批次的取货码
    		ylPickCodeService.activeYlPickCodeByBatchId(Long.valueOf(batchId));
    	}
    	return 1;
    }
}
